using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

public class ZombiesAreaWave : MonoBehaviour
{
	public Transform player;

	public Text numZombies;

	public float[] walls = new float[0];

	public float scaleWalls = 1f;

	public float finishWall = 20f;

	public float waveTime;

	public string sceneComplete;

	private ScreenTitleLevel titleLevel;

	private Vector3 startLocalPosition = default(Vector3);

	private ManagerZombies[] managerZombies;

	private int currentWall;

	private int totalZombies;

	private float scaleWall;

	private float[] startTotal = new float[0];

	private float[] startPeriod = new float[0];

	private float[] startLimit = new float[0];

	private float[] startSpeed = new float[0];

	private float currentWaveTime;

	private float scaleWaveTime;

	private float timerCounter;

	private int p;

	private int z;

	private Vector3 line = default(Vector3);

	private void OnDrawGizmos()
	{
		if (base.enabled)
		{
			line = base.transform.position;
			Gizmos.color = new Color(0f, 1f, 1f);
			Gizmos.DrawLine(new Vector3(finishWall, line.y, line.z), new Vector3(finishWall, line.y + 14f, line.z));
			Gizmos.color = new Color(0f, 1f, 0.5f);
			for (p = 0; p < walls.Length; p++)
			{
				Gizmos.DrawLine(new Vector3(walls[p] * scaleWalls, line.y, line.z), new Vector3(walls[p] * scaleWalls, line.y + 14f, line.z));
			}
		}
	}

	private void Start()
	{
		titleLevel = UnityEngine.Object.FindObjectOfType<ScreenTitleLevel>();
		titleLevel.ShowStart();
		Player.listenersDeath = OnDeathPlayer;
		startLocalPosition = player.InverseTransformPoint(base.transform.position);
		managerZombies = base.gameObject.GetComponents<ManagerZombies>();
		startTotal = new float[managerZombies.Length];
		startPeriod = new float[managerZombies.Length];
		startLimit = new float[managerZombies.Length];
		startSpeed = new float[managerZombies.Length];
		for (z = 0; z < managerZombies.Length; z++)
		{
			startTotal[z] = managerZombies[z].total;
			startPeriod[z] = managerZombies[z].periodAdd;
			startLimit[z] = managerZombies[z].limitOnStage;
			startSpeed[z] = managerZombies[z].scaleSpeedMove;
			managerZombies[z].timeStart = 0f;
			managerZombies[z].quantity = 0;
			managerZombies[z].total = 0;
			managerZombies[z].numBreakLeg = -1;
		}
	}

	private void Update()
	{
		if (currentWall != walls.Length)
		{
			float num = walls[currentWall] * scaleWalls;
			Vector3 position = player.position;
			if (num < position.x)
			{
				currentWall++;
				scaleWall = currentWall;
				scaleWall /= walls.Length;
				currentWaveTime = waveTime;
				timerCounter = 2f;
				UnityEngine.Debug.Log("currentWall:" + currentWall.ToString());
				for (z = 0; z < managerZombies.Length; z++)
				{
					managerZombies[z].quantity = 1;
					managerZombies[z].periodAdd = Mathf.Lerp(startPeriod[z], 0f, scaleWall);
					managerZombies[z].total += Mathf.CeilToInt(Mathf.Lerp(1f, startTotal[z], scaleWall));
					managerZombies[z].limitOnStage = Mathf.CeilToInt(Mathf.Lerp(1f, startLimit[z], scaleWall));
					managerZombies[z].scaleSpeedMove = Mathf.Lerp(startSpeed[z], managerZombies[z].scaleSpeedMoveMax, scaleWall);
				}
			}
		}
		currentWaveTime -= Time.deltaTime;
		scaleWaveTime = Mathf.Clamp01(currentWaveTime / waveTime);
		for (z = 0; z < managerZombies.Length; z++)
		{
			managerZombies[z].periodScale = scaleWaveTime;
		}
		base.transform.position = Vector3.Max(base.transform.position, player.position + startLocalPosition);
		if (Mathf.Floor(Time.time) % 2f == 0f || timerCounter == 2f)
		{
			totalZombies = 0;
			for (z = 0; z < managerZombies.Length; z++)
			{
				totalZombies += managerZombies[z].totalAlive;
			}
			numZombies.text = totalZombies.ToString();
		}
		if (timerCounter != 0f)
		{
			timerCounter = Mathf.Max(0f, timerCounter - Time.deltaTime);
			numZombies.gameObject.SetActive(Mathf.Floor(timerCounter * 4f) % 2f == 0f);
		}
		if (currentWall == walls.Length && ManagerZombies.aliveSceneTotal == 0)
		{
			float num2 = finishWall;
			Vector3 position2 = player.position;
			if (num2 < position2.x)
			{
				titleLevel.ShowCompelete(LoadScene);
				base.enabled = false;
			}
		}
	}

	private void OnDestroy()
	{
	}

	public void OnDeathPlayer()
	{
		Invoke("LoadScene", 4f);
	}

	public void LoadScene()
	{
		GameResult.SetParametr("lastWave", currentWall);
		SettingsStartGame.ResultGame(ManagerZombies.kills, Player.onStage.scores, Mathf.CeilToInt(Player.onStage.moneyHits));
		SettingsStartGame.GameOver(resetWeapon: false);
		SceneManager.LoadScene(sceneComplete);
	}
}
